package com.wwwarehouse.contract.contract.publishgoods;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.adapter.pop.BubblePopAdapter;
import com.wwwarehouse.common.bean.filter.FilterBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.PopUpUtils;
import com.wwwarehouse.contract.bean.ResourceItemBean;
import com.wwwarehouse.contract.bean.SettingRuleBean;
import com.wwwarehouse.contract.contract.MyBaseViewPagerFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.OperationEvent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Description: 优化发布商品(丢雷楼谋版)
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/11/20 14:36
 */
public class PublishGoodSFragment extends MyBaseViewPagerFragment {
    private List<FilterBean> sortList; //排序
    private boolean isShow = true;
    private String mSort = "create_time desc";
    private String pbUkid;
    private String buId;
    private List<SettingRuleBean.DataBean.ListBean> select;
    private List<ResourceItemBean.DataBean.ListBean> selectList = new ArrayList<>();
    private List<ResourceItemBean.DataBean.ListBean> selectData;
    private String text = "";

    @Override
    public void setTitle() {
        if (peekFragment() instanceof PublishGoodSFragment) {
            mActivity.setTitle(getString(R.string.contract_choose_goods));
        }
    }

    @Override
    public void requestDatas() {
        EventBus.getDefault().register(this);
        sortList = new ArrayList<>();
        sortList.add(new FilterBean(true, "更新时间由近到远")); //默认排序
        sortList.add(new FilterBean(false, "更新时间由远到近"));
        Bundle bundle = getArguments();
        if (bundle != null) {
            pbUkid = bundle.getString("pbUkid");
            buId = bundle.getString("buId");
            select = (List<SettingRuleBean.DataBean.ListBean>) bundle.getSerializable("selectList");
            selectData = (List<ResourceItemBean.DataBean.ListBean>) getArguments().getSerializable("select");
            if (select != null) {
                selectList.clear();
//                selectList.addAll(selectData);
                mConfirmBtn.setVisibility(select.size() > 0 ? View.VISIBLE : View.GONE);
                mConfirmBtn.setText(select.size() + "");
            }
        }

        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存选择资源
                ((BaseCardDeskActivity) mActivity).hideSearchTitle();
                SettingPublishingRulesFragment rulesFragment = new SettingPublishingRulesFragment();
                Bundle bundle = new Bundle();
                bundle.putString("pbUkid", pbUkid); //  发布ukid
                bundle.putString("sort", mSort); //   排序(DESC,ASC)
                bundle.putSerializable("selectList", (Serializable) selectList);
                rulesFragment.setArguments(bundle);
                pushFragment(rulesFragment);

            }
        });
        requestData();
    }

    private void requestData() {
        Map<String, Object> map = new HashMap<>();
        map.put("buId", buId); //   业务单位ID(必填)
        map.put("page", "1"); //
        map.put("pbUkid", pbUkid); //  发布ukid
        map.put("rsName", ""); // 资源名称
        map.put("size", "5"); //
        map.put("sort", mSort); //     排序(DESC,ASC)
        httpPost(ContractConstant.RESOURCEITEMS, map, 0, false, "");
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                ResourceItemBean.DataBean dataBean = JSON.parseObject(commonClass.getData().toString(), ResourceItemBean.DataBean.class);
                if (dataBean.getTotal() == 0) {
                    ShowEmptyGoodsView();
                    mConfirmBtn.setText(selectList.size() > 0 ? selectList.size() + "" : getString(R.string.save));
                } else {
                    showViewPagerView();
                    Bundle bundle = new Bundle();
                    bundle.putString("buId", buId); //   业务单位ID(必填)
                    bundle.putString("pbUkid", pbUkid); //  发布ukid
                    bundle.putString("rsName", text); // 资源名称
                    bundle.putString("sort", mSort); //     排序(DESC,ASC)
                    bundle.putSerializable("selectList", (Serializable) selectList); //
                    SetFragmentAndPage(dataBean.getTotal(), 5, new PublishGoodsListFragment().getClass().getName(), bundle);
                }
            } else {
                toast(commonClass.getMsg());
            }
        }


    }

    @Override
    public void sortClick(View anchor) {
        //排序
        PopUpUtils.showBubbleDown(anchor, mActivity, sortList, new BubblePopAdapter.OnPopListClickListener() {
            @Override
            public void setOnItemClickListener(int i, View view) {
                if (i == 0) {
                    mSort = "create_time desc";      //时间正序
                    sortList.get(0).setSelect(true);
                    sortList.get(1).setSelect(false);

                } else if (i == 1) {
                    mSort = "create_time asc";        //时间倒序
                    sortList.get(0).setSelect(false);
                    sortList.get(1).setSelect(true);

                }
                requestData();

            }
        });

    }

    @Override
    public void searchNoRealTimeClick(String text) {
        this.text = text;
        Map<String, Object> map = new HashMap<>();
        map.put("buId", buId); //   业务单位ID(必填)
        map.put("page", "1"); //
        map.put("pbUkid", pbUkid); //  发布ukid
        map.put("rsName", text); // 资源名称
        map.put("size", "5"); //
        map.put("sort", mSort); //     排序(DESC,ASC)
        httpPost(ContractConstant.RESOURCEITEMS, map, 0, true, "");

    }


    public void onEventMainThread(Object event) {
        if (event instanceof OperationEvent) {
            if ("add".equals(((OperationEvent) event).getOperation())) {
                selectList.add(((OperationEvent) event).getBean());
            } else if ("delete".equals(((OperationEvent) event).getOperation())) {
                for (int i = 0; i < selectList.size(); i++) {
                    if (selectList.get(i).getDefinedUkid() == ((OperationEvent) event).getBean().getDefinedUkid()) {
                        selectList.remove(i);
                        break;
                    }
                }
            } else if ("deleteAndRefersh".equals(((OperationEvent) event).getOperation())) {
                for (int i = 0; i < selectList.size(); i++) {
                    if (selectList.get(i).getDefinedUkid() == ((OperationEvent) event).getBean().getDefinedUkid()) {
                        selectList.remove(i);
                        break;
                    }
                }
            }
            if (select != null) {
                mConfirmBtn.setVisibility(selectList.size() + select.size() > 0 ? View.VISIBLE : View.GONE);
                mConfirmBtn.setText((selectList.size() + select.size()) + "");
            } else {
                mConfirmBtn.setVisibility(selectList.size() > 0 ? View.VISIBLE : View.GONE);
                mConfirmBtn.setText(selectList.size() + "");
            }

        }


    }

    @Override
    public void cancelClick() {
        text = "";
        requestData();
    }

    @Override
    public void onDownPull() {
        if (isShow) {
            ((BaseCardDeskActivity) mActivity).hideFilterBt();
            ((BaseCardDeskActivity) mActivity).showSearchTitle();
        } else {
            ((BaseCardDeskActivity) mActivity).hideSearchTitle();
        }
        isShow = !isShow;
    }


}
